require './../../../../libs/styles/list.sass'
require './../../../../libs/styles/messagePush.sass'
require 'ng-table-css'
require 'datepicker-css'
require 'datepicker3-css'
require 'bootstrap-datepicker'

module.exports = angular.module 'messagePush.list', [
  require('ng-table').name
  require('dialogService').name
  require('sessionService').name
]
.controller 'MessagePushListCtrl', ($log, $rootScope, $scope, $location, $timeout, NgTableParams, DialogService, Constant, SessionService)->
  $log.log "messagePush list controller init"
  commonService  = $scope.commonService
  messagePushService   = $scope.messagePushService
  sessionService = new SessionService()
  dialogService = new DialogService()
  constant = new Constant()
  options = {}
  args = {}

  _config = ->
    $scope.pushObj =
      "":"全部"
      "info.yihua.manager":"工长APP"
      "info.yihua.master":"装修APP"
    $scope.searchStatuses =
      "":   "全部"
      true: "已发送"
      false:"待发送"
    $scope.search =
      "key":""
      "isSent":""
      "appPackage":""
      "beginDate":""
      "endDate":""
    $scope.select = "category": "disabled": false
    options = "page": 1, "pageSize": 10
    args =
      total: 0
      getData: (params)->
        searchTemp = {}
        search = angular.copy $scope.search
#        search.beginDate = "#{search.beginDate} 00:00:00" if search.createdBegin
#        search.endDate = "#{search.endDate} 23:59:59" if search.createdEnd
        commonService.copyExcludeEmptyString search, searchTemp
        params.page 1 if $scope.page is 1
        searchTemp.pageSize = params.count()
        searchTemp.page = params.page()
        $location.search searchTemp
        messagePushService.list $location.search(), (data)->
          params.total data.count
          $scope.messagePushs = data.results

    $scope.$watch "search", (newVal)->
      return if !newVal
      searchTemp = {}
      commonService.copyExcludeEmptyString $scope.search, searchTemp
    , true

    $timeout ->
      $("#searchTime").datepicker
        orientation: "bottom left"
        format: "yyyy-mm-dd"
        language: "zh-CN"
        clearBtn: true
    , 0

  _load = ->
    $log.log "list"
    $scope.messagePushTable = new NgTableParams angular.extend(options, $location.search()), args
  _config()
  _load()
  $scope.queryAction = ->
    $scope.page = 1
    $scope.messagePushTable.reload()
    $scope.page = ""
  $scope.stop =  (id,disabled,index)->
    $scope.updata =
      "disabled":""
    $scope.updata.disabled = if disabled is true then false else true
    messagePushService.update {"id": id}, $scope.updata, (data)->
      $log.log data
      $scope.messagePushs[index].disabled = if disabled is true then false else true

  _goto = (filename, url)->
    iframe = document.createElement "iframe"
    iframe.style.display = 'none'
    iframe.src = url
    document.body.appendChild iframe
    iframe.contentWindow.document.execCommand('SaveAs', false, filename);
